package com.rongzhe.house.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.rongzhe.house.utils.UIUtils;

import java.util.List;

/**
 * Created by huang on 2017/12/25.
 */

public abstract class MyBaseAdapter<T> extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mLayoutInFlater;

    public MyBaseAdapter(Context context) {
        mContext = context;
        mLayoutInFlater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public List<T> mData;

    public void setData(List<T> data) {
        if (mData != null) {
            mData.clear();
        }
        this.mData = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData == null ? null : mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mLayoutInFlater.inflate(android.R.layout.simple_list_item_1,
                    parent, false);
            ViewGroup.LayoutParams layoutParams = convertView.getLayoutParams();
            layoutParams.height = UIUtils.dip2px(mContext, 40);
            convertView.setLayoutParams(layoutParams);
        }
        T itemData = mData.get(position);
        ((TextView) convertView).setText(getShowingData(itemData));
        return convertView;
    }

    public abstract String getShowingData(T t);
}
